package com.lgx.mall.core.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.json.Json2Utils;
import com.lgx.common.web.util.DateUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.basic.entity.News;
import com.lgx.mall.basic.service.INewsService;
import com.lgx.right.entity.User;

@Controller
@RequestMapping("/news")  
public class NewsController {
	
	@Autowired
	private INewsService newsService;

	public INewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(INewsService newsService) {
		this.newsService = newsService;
	}
	
	/**
	 * news管理
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/newsMgr.do")  
	public String userMgr(HttpServletRequest request,HttpServletResponse response){
		String type = request.getParameter("type");
		request.setAttribute("type", type);
		return "mall/basic/news/newsMgr";
	}
	
	
	/**
	 * list.do页面开启加载，无需返回
	 * type =1新闻
	 * type =2公告
	 * type =3新手上路
	 * @return
	 */
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response){
		
		String starTime = request.getParameter("starTime");
		String endTime = request.getParameter("endTime");
		String author = request.getParameter("author");
		String type = request.getParameter("type");                //信息类型
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = Constants.PAGE_SIZE;
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		
		User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);   //自动抓取当前登录用户的信息
		if(user != null){
			Pagination page = this.newsService.findNewsPagination(Integer.parseInt(type),starTime,endTime,author,Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		    Json2Utils.parseJSON(page, response);
		}
	}
	
	//跳转修改
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response){
		String newsId = request.getParameter("newsId");  //接受新闻id（适用于编辑）
		String otype = request.getParameter("otype");      //接受操作type
		String type = request.getParameter("type");
		
		if("edit".equals(otype)){                         //如果操作状态为编辑，则把勾选的用户信息作为一个对象传送
			News news = this.newsService.findNewsById(Long.parseLong(newsId));
			request.setAttribute("news", news);
		}
		
		//传入必要的操作类型和操作id
		request.setAttribute("otype", otype);
		request.setAttribute("newsId", newsId);
		request.setAttribute("type", type);
		
		return "mall/basic/news/news-init";
	}
	
	
	/**
	 * 处理修改或者是添加
	 * type =1新闻
	 * type =2公告
	 * type =3新手上路
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response){
		String type = request.getParameter("type");    
		
		String title = request.getParameter("title");
		if(!StringUtils.isEmpty(title))title = EscapeUtils.unescape(title);
		
		String content = request.getParameter("content");
		if(!StringUtils.isEmpty(content))content = EscapeUtils.unescape(content);
		
		String author = request.getParameter("author");   //编辑使用 
		if(!StringUtils.isEmpty(author))author = EscapeUtils.unescape(author);
		
		String time = request.getParameter("time");
		if(!StringUtils.isEmpty(time))time = EscapeUtils.unescape(time);
		
		String layerOrder = request.getParameter("layerOrder");
		if(!StringUtils.isEmpty(layerOrder))layerOrder = EscapeUtils.unescape(layerOrder);
		
		
		//新添加用户的信息
		News news = new News();
		
		news.setType(Integer.parseInt(type));      						 // 1新闻2公告3新手上路
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);    
		news.setTime(DateUtils.charToDate(time,DateUtils.DATE_FORMAT_T));

		if(layerOrder.equals(null)){
			news.setLayerOrder("");
		}else{
			news.setLayerOrder(layerOrder);
		}
		
		
		 try {
			newsService.createNews(news);   
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
			return "1";
	}
	
	//修改实现
	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response){
		String newsId = request.getParameter("newsId");
		String type = request.getParameter("type");
		
		String title = request.getParameter("title");
		if(!StringUtils.isEmpty(title))title = EscapeUtils.unescape(title);
		
		String content = request.getParameter("content");
		if(!StringUtils.isEmpty(content))content = EscapeUtils.unescape(content);
		
		String author = request.getParameter("author");   //编辑使用 
		if(!StringUtils.isEmpty(author))author = EscapeUtils.unescape(author);
		
		String time = request.getParameter("time");
		if(!StringUtils.isEmpty(time))time = EscapeUtils.unescape(time);
		
		String layerOrder = request.getParameter("layerOrder");
		if(!StringUtils.isEmpty(layerOrder))layerOrder = EscapeUtils.unescape(layerOrder);
		
		
		//新添加用户的信息 
		News news = new News();
		
		news.setId(Long.parseLong(newsId));          //传入id
		news.setType(Integer.parseInt(type));                             //如果不是删除,始终把类型保持为1
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);    					 //作者，操作者手动输入
		news.setTime(DateUtils.charToDate(time,DateUtils.DATE_FORMAT_T));   //时间
		news.setLayerOrder(layerOrder);
		
		try {
			news = newsService.updateNews(news);
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
			
			return "1";
		
	}
	
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response){
		String ids = request.getParameter("ids");
		for(String id : ids.split(",")){
			try {
				News ns = this.newsService.findNewsById(Long.parseLong(id));   //查看要删除的新闻是否存在
				ns.setId(Long.parseLong(id));
				this.newsService.deleteNews(ns);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				return "0";
			} catch (DataException e) {
				e.printStackTrace();
				return "0";
			}
		}
		return "1";
	}
	

}
